// =============================================================
// space test btw. server/client
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Oct. 8, 2007
// =============================================================
#include <iostream>

#include <sys/timeb.h>
#include <time.h>

#include "common_coord.h"

#include "property/CProperty.h"

#include "CWorkSpaces.h"

// space proxy
pair<string, string> &cube_space_op(char *host, int port, char *interaction_service, char *interaction_service_instance, int op, pair<string,string> &keyval)
{
	CWorkSpaces *wspace = new CWorkSpaces(host, port, interaction_service, interaction_service_instance);
	wspace->autoclean(false);

	if(wspace->alive()==false) {
		cerr << "there is no coord to run" << endl;
		exit(-1);
	}

	if(op==0) wspace->readIfExists(keyval);
	else if(op==1) wspace->takeIfExists(keyval);
	else if(op==2) wspace->write(keyval);

	delete wspace;

	return keyval;
}

void print_usage(char *prog)
{
	cout << "-------------------------------------------------------------------------" << endl;
	cout << "Usage : " << prog << " [-c <config> or -h <ip> -p <port>] -r <key to read> -t <key to take> -w <key,value to write> [-s <service> -i <instance>]" << endl;
	cout << "\t\t-r\tread from space" << endl;
	cout << "\t\t-t\ttake from space" << endl;
	cout << "\t\t-w\twrite to space" << endl;
	cout << endl;
	cout << "-------------------------------------------------------------------------" << endl;
}

int main(int argc, char **argv)
{
	if(argc<2) {
		print_usage(argv[0]);
		exit(-1);
	}

	int opt;
	static char optstr[] = "chprtwsi";

	char *conf = "coord.conf";
	string host = "";
	int port = -1;
	int op = 0; // read by default
	char *key = NULL;
	char *value = NULL;
	char *ispace = CUBE_SERVICE_SPACE;
	char *ispace_instance = CUBE_SERVICE_SPACE;

	while((opt=getopt(argc,argv,optstr))!=-1) {
		switch(opt) {
			case 'c' :
			{
				conf = argv[optind++];
				CProperty config(conf);
				pair<string,string> kv;
                                kv.first = COORD_CONF_MASTER_IP_KEY;
                                host = config.get(kv).second;
                                kv.first = COORD_CONF_MASTER_PORT_KEY;
                                port = atoi((char *)config.get(kv).second.c_str());
				break;
			}
			case 'h' :
				host = argv[optind++];
				break;
			case 'p' :
				port = atoi(argv[optind++]);
				break;
			case 'r' :
				key = argv[optind++];
				op = 0; // read operation
				break;
			case 't' :
				key = argv[optind++];
				op = 1; // take operation
				break;
			case 'w' :
				key = argv[optind++];
				value = argv[optind++];
				op = 2; // write operation
				break;
			case 's' :
				ispace = argv[optind++];
				if(ispace==NULL) ispace_instance = ispace;
				break;
			case 'i' :
				ispace_instance = argv[optind++];
				break;
			default :
				print_usage(argv[0]);
				exit(-1);
		}
	}

	if(port>0&&host!="") {
		if(key!=NULL) {
			pair<string,string> keyval;
			keyval.first = key;
			if(value!=NULL) keyval.second = value;

			keyval = cube_space_op((char *)host.c_str(), port, ispace, ispace_instance, op, keyval);

			if(keyval.second!="") {
				if(op==0) cout << "############################# read ###############################" << endl;
				else if(op==1) cout << "############################# take ###############################" << endl;
				else if(op==2) cout << "############################# write ##############################" << endl;
				cout << "key = " << keyval.first << endl;
				cout << "value = " << keyval.second << endl;
				cout << "##################################################################" << endl;
			}
			else {
				cout << "there is no data : key = " << keyval.first << endl;
			}
		}
		else print_usage(argv[0]);
	}
	else print_usage(argv[0]);

        return 1;
}
